﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
	<div class="layui-form-item">
		<label class="layui-form-label">权限名称</label>
		<div class="layui-input-block">
			<input type="text" name="permissionName" lay-verify="required" placeholder="请输入权限名称" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">父级权限名称</label>
		<div class="layui-input-block">
			<select id="parentname" name="parentId">
				<option value="0">请选择</option>
			</select>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">图标</label>
		<div class="layui-input-block">
			<input type="text" name="icon" lay-verify="required" placeholder="请输入图标" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">页面地址</label>
		<div class="layui-input-block">
			<input type="text" name="permissionUrl" lay-verify="required" placeholder="请输入图标" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">排序</label>
		<div class="layui-input-block">
			<input type="number" name="sortId" lay-verify="required" placeholder="请输入排序" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">是否启用</label>
		<div class="layui-input-block">
			<input type="radio" name="permissionStatus" value="1" title="启用" checked>
			<input type="radio" name="permissionStatus" value="2" title="禁用">
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>
	layui.use(['form', 'laydate'], function(){
	  var form = layui.form;
	  var layer = layui.layer;
	  var laydate = layui.laydate;

	//绑定父级权限下拉框
	function bindmaterials(){
		$.ajax({
			url:'/RBAC/GetPermissionList',
			type:'get',
			dataType:'json',
			success:function(res){
				$(res.data).each(function () {
					  $("#parentname").append('<option value="' + this.id + '">' + this.permissionName + '</option>')
				  })
				  layui.form.render();
			}
		})
	}

	bindmaterials()

	  // 提交事件
	  form.on('submit(demo1)', function(data){
		var field = data.field; // 获取表单字段值
		// 此处可执行 Ajax 等操作
		console.log(field)
		$.ajax({
			url:'/RBAC/AddPermission',
		   data:field,
		   type:'post',
		   dataType:'text',
		   success:res=>{
			   if (res > 0) {
				  //layer信息提示
				  layer.alert('添加成功');//下面是关键代码
				  var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				  window.parent.location.reload(); // 刷新父页
				  parent.layer.close(index); //再执行关闭
				  setTimeout(function () { parent.layer.close(index) }, 30000);//延迟s
			   }
			   else {
				  layer.alert("添加失败！");
			   }
		   }
		})
		return false; // 阻止默认 form 跳转
	  });
	});
</script>

